home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5915 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: solon.com!not-for-mail
  2. From: Gihan Perera <Gihan@andante.demon.co.uk>
  3. Newsgroups: comp.lang.c.moderated,comp.lang.c
  4. Subject: Re: Please help me elect rounding of int division
  5. Date: 21 Feb 1996 18:00:09 -0600
  6. Organization: Rick's Cafe
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4ggbm9$85t@solutions.solon.com>
  10. References: <4gfaif$1tt@solutions.solon.com>
  11. Reply-To: Gihan@andante.demon.co.uk
  12. NNTP-Posting-Host: solutions.solon.com
  13. X-NNTP-Posting-Host: andante.demon.co.uk
  14. X-Newsreader: Demon Internet Simple News v1.30
  15. X-Mail2News-Path: andante.demon.co.uk
  16.  
  17. In article <4gfaif$1tt@solutions.solon.com>
  18.            swedecj@vcnet.com "Carl Jacobson" writes:
  19.  
  20. > Please help me solve this task.
  21. > I have now tried (Borland C++ version 3.0) for two solid days to write
  22. > a function that would allow me to round the quotient of (a/b) up or
  23. > down based on the "nearest" integer instead of being truncated to the
  24. > smallest. If exactly half way, return the "odd."
  25.  
  26. The first part is easy.  If you're using reals, add 0.5 to (a/b) before
  27. truncating it.  If you're doing the whole thing with integers, use:
  28.  
  29.    q = ( a + b/2 ) / b;
  30.  
  31. which has the same effect.
  32.  
  33. But neither of these meets your second criterion ("return the odd") -
  34. they both round _up_ if exactly half-way.  Sorry - I don't know any
  35. easy trick for that.
  36.  
  37. Gihan
  38.  
  39. -----------------------------------+----------------------------------------
  40. | Gihan Perera                     |  Me--> * U.K.                         |
  41. |                                  |                                       |
  42. | Email: gihan@andante.demon.co.uk |                             ,-_|\     |
  43. |                                  |                            /     \    |
  44. |                                  |                    Home--> *_,-._/    |
  45. |                                  |                                 v     |
  46. -----------------------------------+----------------------------------------
  47.